
package com.redteam.data.dao;

import com.redteam.data.beans.RentalItem;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Contains methods for <code>RentalItems</code> DAO
 * @author Jezreel
 */
public class RentalItemDAO extends AbstractDAO implements DAO {

    private static RentalItemDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new RentalItemDAO();
    }

    private RentalItemDAO() {
        getDataSource();
        tableName = "HIGHFLIERS.RentalItems";
    }

    /**
     * Static method for getting <code>RentalItemDAO</code>
     * @return
     */
    public static RentalItemDAO getInstance() {
        return dao;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(RentalItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object obj) {
        RentalItem item = (RentalItem) obj;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(getTableName()).append(" SET RATE = ").append(item.getRate());
        sb.append(" WHERE ID = ").append(item.getId());
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public RentalItem getObjectOut(ResultSet rs) {
        RentalItem item = new RentalItem();
        try {
            item.setId(rs.getLong("ID"));
            item.setFboID(rs.getLong("FBO_ID"));
            item.setType(rs.getString("DISCRIMINATOR"));
            item.setRate(rs.getDouble("RATE"));
            item.setAircraftType(rs.getString("TYPE"));
        } catch (SQLException ex) {
            Logger.getLogger(RentalItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return item;
        }

    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object obj) {
        RentalItem item = (RentalItem) obj;
        StringBuilder sb = new StringBuilder("INSERT INTO ").append(this.getTableName());
        sb.append(" (FBO_ID,RATE,TYPE,DESCRIMINATOR");
        sb.append(") VALUES (").append(item.getFboID()).append(",").append(item.getRate());
        sb.append(",'").append(item.getAircraftType()).append("','").append(item.getType()).append("')");
        return sb.toString();
    }

    /**
     * Gets <code>PersonType</code> for specified <code>Person</code>
     * @param personId
     * Id of <code>Person</code>
     * @return
     * PersontType
     */
    public String itemType(Long itemId) {
        RentalItem item = null;
        try {
            item = (RentalItem) this.getSingle(itemId);
        } catch (SQLException ex) {
            Logger.getLogger(RentalItemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item.getType();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object o) {
        RentalItem item = (RentalItem) o;
        return item.getId();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return RentalItemDAO.ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return tableName;
    }
}
